home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / StyleWriter Page Monitor GX / Source / logIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  3.1 KB  |  123 lines  |  [TEXT/MMCC]

  1. /***
  2.  * logIO.c
  3.  *
  4.  *  IO routines to the log file
  5.  *
  6.  ***/
  7.  
  8. #include "Headers.h"
  9.  
  10. #include "PageMonRes.h"
  11. #include "logIO.h"
  12. #include "Folders.h"
  13. #include <Errors.h>
  14. #include "Exceptions.h"
  15.  
  16. #define    NEW_LINE            0xD                // New line character
  17.  
  18. /**
  19.  * WriteToLog
  20.  *
  21.  *  Write a line out to the log file detailing the number of pages we are about to print.
  22.  *  Find this thing in the Pref folder, under Printer Prefs. :)
  23.  *
  24.  **/
  25.  
  26. OSErr WriteToLog (long numPages)
  27. {
  28.     OSErr        anErr;
  29.  
  30.     FSSpec    styleLogFile;
  31.     short        refNum;
  32.     long        writeSize;
  33.  
  34.     Str255    string;
  35.     Str255    stringLine;
  36.  
  37.     /**
  38.      ** Load in the string resource that contains the path of the file from the
  39.      ** prefs folder to the log file
  40.      **/
  41.     
  42.     string[0] = 0;
  43.     GetIndString (string, PAGE_MON_STRS, PAGE_MON_LOG_PATH);
  44.     anErr = ResError ();
  45.     if (anErr == noErr && string[0] == 0) anErr = -1;
  46.     stringLine[0] = 0;
  47.     if (anErr == noErr) {
  48.         GetIndString (stringLine, PAGE_MON_STRS, PAGE_MON_PG_MESSAGE);
  49.         anErr = ResError ();
  50.         if (anErr == noErr && stringLine[0] == 0) anErr = -1;
  51.     }
  52.     nrequire (anErr, FailedFindResStrs);
  53.  
  54.     /**
  55.      ** Great.  Now find out where the style writer II log is located -- get an fsspec for
  56.      ** the thing.  Use find folder to do this...  Note, we have to setup a a4 world
  57.      ** for a few seconds while we do this cause we have to get access to a couple of strings!
  58.      **/
  59.  
  60.     anErr = FindFolder (kOnSystemDisk, kPreferencesFolderType, false,
  61.                                     &(styleLogFile.vRefNum),
  62.                                     &(styleLogFile.parID));
  63.     nrequire (anErr, FailedFindPrefFolder);
  64.  
  65.     if (string[0] > 63) string[0] = 63;
  66.     BlockMove (string, styleLogFile.name, string[0]+1);
  67.  
  68.     /**
  69.      ** We have an fsspec that "should" be valid at this point.  Next, we
  70.      ** just have to see if the file exists.  If so, we can create it... 
  71.      **/
  72.  
  73.     anErr = FSpCreate (&styleLogFile, 'ttxt', 'TEXT', smSystemScript);
  74.     require (anErr == noErr || anErr == dupFNErr, FailedFileCreate);
  75.  
  76.     /**
  77.      ** Now we also have a created file.  Next job is to open it for writing
  78.      **/
  79.  
  80.     anErr = FSpOpenDF (&styleLogFile, fsWrPerm, &refNum);
  81.     nrequire (anErr, FailedOpenFile);
  82.     anErr = SetFPos (refNum, fsFromLEOF, 0);
  83.     nrequire (anErr, FailedMoveEOF);
  84.  
  85.     /**
  86.      ** Build the string that we are going to stuff into the file
  87.      **/
  88.  
  89.     NumToString (numPages, string);
  90.     BlockMove (&(string[1]), &(stringLine[stringLine[0]+1]), string[0]);
  91.     stringLine[0] += string[0];
  92.  
  93.     stringLine[0] += 1;                                        // Add a newline!
  94.     stringLine[stringLine[0]] = NEW_LINE;
  95.  
  96.     /**
  97.      ** Write that puppy out
  98.      **/
  99.     
  100.     writeSize = stringLine[0];
  101.     anErr = FSWrite (refNum, &writeSize, &(stringLine[1]));
  102.     nrequire (anErr, FailedWrite);
  103.  
  104.     /**
  105.      ** If we drop to here, we have no error to deal with... :)
  106.      **/
  107.  
  108.     FSClose (refNum);
  109.     return noErr;
  110.  
  111.     /**
  112.      ** Handle a small :) number of errors
  113.      **/
  114.  
  115. FailedWrite:                                        // Failed to do the write
  116. FailedMoveEOF:                                        // Couldn't move to end of file!
  117.     FSClose (refNum);
  118. FailedOpenFile:                                    // Didn't open log file!
  119. FailedFileCreate:                                    // We could not create the file for some reason!
  120. FailedFindPrefFolder:                            // No prefs folder on this machine
  121. FailedFindResStrs:                                // Didn't get all the strings back!
  122.     anErr = noErr;
  123. }